package com.ai.module.business.service.impl;

import com.ai.module.chat.common.base.ThreadLocalContext;
import com.ai.module.chat.common.constant.RedisKeyStringConstant;
import com.ai.module.chat.config.advice.exception.BusinessException;
import com.ai.module.business.domain.entity.BusinessRecordEntity;
import com.ai.module.business.domain.entity.BusinessTotalEntity;
import com.ai.module.business.domain.req.BusinessTotalReq;
import com.ai.module.business.domain.vo.BusinessTotalVo;
import com.ai.module.business.mapper.BusinessRecordMapper;
import com.ai.module.business.mapper.BusinessTotalMapper;
import com.ai.module.business.service.BusinessTotalService;
import com.ai.module.business.service.ImageGroundService;
import com.ai.module.user.domain.entity.ImageGroundEntity;
import com.ai.module.chat.util.DateUtils;
import com.ai.module.chat.util.RedisTemplateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: MrHe
 * @create: 2024-01-25 02:06
 * @description:
 **/
@Service
public class BusinessTotalServiceImpl extends ServiceImpl<BusinessTotalMapper, BusinessTotalEntity> implements BusinessTotalService {

    @Autowired
    private BusinessRecordMapper businessRecordMapper;

    @Autowired
    private ImageGroundService imageGroundService;

    @Override
    public void collectLike(BusinessTotalReq businessTotal) {
        if (ThreadLocalContext.getUserLoginInfo() == null){
            return;
        }
        String businessId = businessTotal.getBusinessId();
        String businessType = businessTotal.getBusinessType();
        String redisKey =String.format(RedisKeyStringConstant.BUSINESS_TOTAL,businessId);
        BusinessTotalVo businessTotalVo=  RedisTemplateUtils.get(redisKey);
        // 判断 数据库有没有数据
        BusinessRecordEntity businessRecord = businessRecordMapper.selectOne(new LambdaQueryWrapper<BusinessRecordEntity>()
                .eq(BusinessRecordEntity::getBusinessId, businessId)
                .eq(BusinessRecordEntity::getBusinessType, businessType)
        );

        if (businessRecord != null) {
            if (businessType.equalsIgnoreCase("LIKE")) {
                businessTotalVo.setLikeNum(businessTotalVo.getLikeNum()-1);
            }else {
                businessTotalVo.setCollectionNum(businessTotalVo.getCollectionNum()-1);
            }
        }else {
            businessRecord= new BusinessRecordEntity();
            businessRecord.setBusinessId(businessId);
            businessRecord.setBusinessType(businessType);
            businessRecordMapper.insert(businessRecord);
        }


    }

    @Override
    public BusinessTotalVo findBusiness(BusinessTotalReq businessTotalReq) {
        String businessId = businessTotalReq.getBusinessId();

        // 查询用户关于刚才数据对的收藏 喜欢记录
        List<BusinessRecordEntity> businessRecordEntities = businessRecordMapper.selectList(
                new LambdaQueryWrapper<BusinessRecordEntity>().eq(
                        BusinessRecordEntity::getUserId, ThreadLocalContext.getUserId())
        );
        String redisKey =String.format(RedisKeyStringConstant.BUSINESS_TOTAL,businessId);
        BusinessTotalVo businessTotalVo=  RedisTemplateUtils.get(redisKey);
        if (businessTotalVo ==null){
            BusinessTotalEntity businessTotal = getOne(
                    new LambdaQueryWrapper<BusinessTotalEntity>()
                            .eq(BusinessTotalEntity::getBusinessId, businessId)
            );
            if (businessTotal==null){
                throw new BusinessException("数据已删,请刷新再试");
            }
            businessTotalVo = new BusinessTotalVo();
            BeanUtils.copyProperties(businessTotal, businessTotalVo);
            RedisTemplateUtils.set(redisKey,businessTotalVo, DateUtils.TIME_DAY);
        }

        BusinessTotalVo finalBusinessTotalVo = businessTotalVo;
        businessRecordEntities.forEach(businessRecord->{
            String businessType = businessRecord.getBusinessType();
            if (businessType.equalsIgnoreCase("LIKE")){
                finalBusinessTotalVo.setLike(1);
            }
            if (businessType.equalsIgnoreCase("COLLECT")){
                finalBusinessTotalVo.setCollect(1);
            }

        });
        // 查询用户数据
        ImageGroundEntity imageGround = imageGroundService.getOne(new LambdaQueryWrapper<ImageGroundEntity>()
                .eq(ImageGroundEntity::getUuid, businessId));
        finalBusinessTotalVo.setPrompt(imageGround.getPrompt());
        finalBusinessTotalVo.setFilePath(imageGround.getFilePath());
        finalBusinessTotalVo.setScalePath(imageGround.getScalePath());
        finalBusinessTotalVo.setPhone(imageGround.getUserId());
        return finalBusinessTotalVo;
    }

    @Override
    public void saveTotalBusiness(String businessId, String businessType) {
        BusinessTotalEntity businessTotal = new BusinessTotalEntity();

        businessTotal.setBusinessId(businessId);
        businessTotal.setBusinessType(businessType);
        this.save(businessTotal);

    }
}
